package com.ka.component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.widget.EditText;

public class UserNameValidation extends IFieldValidation {

	protected EditText ed_title;

	public UserNameValidation(EditText field, Boolean req) {

		ed_title = field;
		require = req;

	}

	public UserNameValidation(EditText field) {
		ed_title = field;
		require = false;
	}

	@Override
	public MessageValidate validate() {

		MessageValidate msg = new MessageValidate();
		String text = ed_title.getText().toString();

		if (!StringUtils.checkInvalidString(text) && require) {
			msg.addErrorMessage("Username invalid!");
			return msg;
		}

		if (!StringUtils.checkMaxValidString(text)) {
			msg.addErrorMessage("Username overflow size (1000).");
			return msg;
		}

		Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(text);
		boolean b = m.find();

		if (b) {
			msg.addErrorMessage("User name contain violation character.");
		}

		return msg;

	}
}
